﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ExcellentCRM.DataService.Exts;
using ExcellentCRM.DataService.Models;
using ExcellentCRM.Site.Models;
using ExcellentCRM.Site.Utilities;
using Webdiyer.WebControls.Mvc;

namespace ExcellentCRM.Site.Controllers
{
    public class StuClientController : BaseController
    {
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Index(SearchStuClientModel sscModel)
        {
            ModelState.Remove("StuClient.StudentInfo.StuName");
            if (ModelState.IsValid)
            {
                sscModel.StuClient.ConsultantId = CurrentStaffId;
                int pageSize = PresetValues.PageSize,
                    pageIndex = sscModel.PageIndex,
                    totalCount;
                var stuClients = 
                    ServiceFactory.StuClientService.Search(sscModel.StuClient, pageSize, pageIndex, out totalCount);
                sscModel.SearchResult =
                    new PagedList<StuClient>(stuClients, pageIndex, pageSize, totalCount);
            }
            return View(sscModel);
        }

        public ActionResult CreateOrUpdate(long? stuClientId)
        {
            var stuClientModel = new StuClientModel();
            if (stuClientId.HasValue && stuClientId.Value > 0)
            {
                var stuClient = ServiceFactory.StuClientService.GetModel(stuClientId.Value);
                stuClientModel.MergeFrom(stuClient);
                stuClientModel.StudentInfo.MergeFrom(stuClient.StudentInfo);
            }
            return PartialView(stuClientModel);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult CreateOrUpdate(StuClientModel stuClientModel)
        {
            if (ModelState.IsValid)
            {
                stuClientModel.ConsultantId = CurrentStaffId;
                var stuClient = new StuClient();
                stuClient.MergeFrom(stuClientModel);
                stuClient.StudentInfo.MergeFrom(stuClientModel.StudentInfo);
                var sr = ServiceFactory.StuClientService.Save(stuClient);
                return Json(new AjaxResult(sr.Success, sr.Message) {Expandos = stuClient});
            }
            return Json(new AjaxResult(false, ModelErrorStr));
        }

        [HttpPost]
        public ActionResult VerifyStuExists([Bind(Prefix = "StudentInfo")] StudentInfo stuInfo, long? id)
        {
            bool isValid =
                !ServiceFactory.StuClientService.StuClientIsExists(stuInfo.StuName, stuInfo.Phone, stuInfo.CellPhone,
                                                                   stuInfo.Email, id);
            return Json(isValid, JsonRequestBehavior.DenyGet);
        }

        public ActionResult ContactRecord(long stuId, long? contactId)
        {
            var contactRecord = new ContactRecord {ConsultantId = CurrentStaffId, ContactStuId = stuId};
            if (contactId.HasValue)
            {
                contactRecord = ServiceFactory.StuClientService.GetContactRecord(contactId.Value);
            }
            return PartialView(contactRecord);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult ContactRecord(ContactRecord contactRecord)
        {
            if (ModelState.IsValid)
            {
                var sr = ServiceFactory.StuClientService.SaveContactRecord(contactRecord);
                return Json(new AjaxResult(sr.Success, sr.Message) {Expandos = contactRecord});
            }
            return Json(new AjaxResult(false, "请将错误处修正再保存"));
        }
    }
}
